package rad.framework.mda.jpagen.actions;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import rad.framework.mda.generator.preferences.Preferences;
import rad.framework.mda.jpagen.GeneratorPlugin;

import org.eclipse.uml2.uml.NamedElement;

@SuppressWarnings("unchecked")
public class NameUtil {

	// TODO sequence names

	public static String getAbbreviation(NamedElement element) {
		List parts = parseName(element.getName());

		StringBuffer result = new StringBuffer();
		for (Iterator nameIter = parts.iterator(); nameIter.hasNext();) {
			String nameComponent = (String) nameIter.next();
			nameComponent = getAbbreviation(nameComponent);
			result.append(nameComponent);
			if (nameIter.hasNext() && nameComponent.length() > 1) {
				result.append("_");
			}
		}
		// check whole name for override
		return getAbbreviation(result.toString());
	}

	/** @deprecated */
	public static String removeVowels(String word) {
		// TODO special cases
		if (word.equals("id")) {
			return word;
		}
		StringBuilder builder = new StringBuilder();
		char[] letters = word.toCharArray();
		for (char c : letters) {
			if (!"aeiouAEIOU".contains("" + c)) {
				builder.append(c);
			}
		}
		return builder.toString();
	}

	public static List parseName(String sourceName) {
		char sourceSeparator = '_';
		List parts = new ArrayList();
		StringBuffer currentWord = new StringBuffer();
		int length = sourceName.length();
		boolean lastIsLower = false;
		for (int index = 0; index < length; index++) {
			char curChar = sourceName.charAt(index);
			if (Character.isUpperCase(curChar)
					|| (!lastIsLower && Character.isDigit(curChar))
					|| curChar == sourceSeparator) {
				if (lastIsLower || curChar == sourceSeparator) {
					parts.add(currentWord.toString());
					currentWord = new StringBuffer();
				}
				lastIsLower = false;
			} else {
				if (!lastIsLower) {
					int currentWordLength = currentWord.length();
					if (currentWordLength > 1) {
						char lastChar = currentWord.charAt(--currentWordLength);
						currentWord.setLength(currentWordLength);
						parts.add(currentWord.toString());
						currentWord = new StringBuffer();
						currentWord.append(lastChar);
					}
				}
				lastIsLower = true;
			}
			if (curChar != sourceSeparator) {
				currentWord.append(curChar);
			}
		}
		parts.add(currentWord.toString());
		return parts;
	}

	public static String getAbbreviation(String word) {
		Properties p = getAbbreviationProperties();
		if (p == null) {
			return word;
		}

		String abbrv = p.getProperty(word);
		if (abbrv == null) {
			return word;
		} else {
			return abbrv;
		}
	}

	private static Properties abbreviationProperties;

	private static Properties getAbbreviationProperties() {
		if (abbreviationProperties == null) {
			try {
				abbreviationProperties = new Properties();
				String name = Preferences.getTemplateRoot()
						+ "/abbreviation.properties";
				FileInputStream fio = new FileInputStream(name);
				abbreviationProperties.load(fio);
			} catch (Exception e) {
				abbreviationProperties = null;
				GeneratorPlugin.getDefault().log(
						"Unable to load abbreviation properties.", e);
			}
		}
		return abbreviationProperties;
	}
	
	public static void initialize() {
		abbreviationProperties = null;
	}
}
